package com.apk.tools;


import java_util.coo_util.C_File;
import java_util.coo_util.Log.C_Console;
import java_util.coo_util.Log.C_Log;
import java_util.coo_util.Log.C_LogTxtSave;

import java.io.File;
import java.util.*;

public class C_main_win32Copy {

    static String rootPath = "D:/Win32Work/Win32Build/Work/";
    static String macShareD = "D:/VMware/macShareD/app2build/";

    /***
     * 0=userId,1=packages8,2=buildType,3=buildId,
     * @param para
     * @throws Exception
     */
    public static void main(String[] para) throws Exception {
        if (para == null || para.length < 2) {
            System.err.printf("错误.........输入的参数长度小于2");
            return;
        }
        System.out.printf("开始处理........" + para[0] + "......." + para[1]);
        String userId = para[0];
        String packages8 = para[1];


        C_Console console = new C_Console().S_Init(true, 10, "win32Copy", rootPath);


        int buildType = 1;
        assert para != null;
        if (para.length >= 3) {
            try {
                buildType = Integer.parseInt(para[2].trim());
                if (buildType != 1) {
                    System.err.print("无法打包....打包类型不明确.." + para[1]);
                    return;
                }
            } catch (Exception ex) {
                System.err.print("解析类型错误......" + para[2]);
            }
        }
        String buildId = "0";
        if (para.length >= 4) {
            buildId = para[3];
        }


        String[] packageArr = packages8.split("[+]");
        String appPath0 = rootPath + "../app/";
        String toolsPath0 = rootPath + "../buildTools/";
        //C_File.S_delete(userPath + "/_build/");


        Map<String, String> workPathDic = new HashMap<>();

        int i = 0;
        for (String package0 : packageArr) {
            String userPath = rootPath + userId + ".1." + package0;
            String workPath = userPath + "/" + package0 + "/";
            String configDir = macShareD + userId + "/" + package0 + "/";

            C_File.S_delete(userPath + "/app/");
            C_File.S_copyDir(appPath0, userPath + "/app/");
            C_File.S_copyDir(toolsPath0, userPath);


            C_File.S_delete(workPath);
            C_File.S_copyDir(configDir, workPath);
            workPathDic.put(package0, workPath);
            C_Log.LogFormat("\n\n\n\n\n拷贝完成.........%d....\n\n\n\n\n", i);
            i++;


            C_cmd.S_Runtime(userPath + "/build.exe");


            File file = new File(userPath + "/_build/");
            C_Log.Log("_build工作路径........" + file.getPath());
            File[] tempList = file.listFiles();
            for (File file0 : tempList) {
                if (file0.isDirectory()) {
                    String path = file0.getPath();
                    if (workPathDic.containsKey(file0.getName())) {
                        C_Log.Log("压缩文件........" + path);
                        build_config setting = new build_config();
                        String wordPath = workPathDic.get(file0.getName());
                        if (!setting.S_LoadData(wordPath)) {
                            return;
                        }
                        setting.S_SetInfo(new File(wordPath), buildId, 1);
                        C_ProgressReport.S_SendProgress(setting, C_ProgressReport.o_fixData);
                        String pathZip = path + ".zip";
                        C_File.S_delete(pathZip);
                        C_Log.Log("压缩文件........" + path);
                        C_cmd.S_Runtime("7z a " + pathZip + "  " + path + "/");

                        C_ProgressReport.S_SendProgress(setting, C_ProgressReport.o_upload, "");

                        C_ProgressReport.S_uploadDNS(setting, pathZip, userId + "/" + userId + ".1." + setting.temp_package + ".zip");
                    } else {
                        C_Log.Log("压缩文件........" + path);
                    }
                }
            }
        }
        C_Log.Log("\n\n\n\n\n\n\n\n\n\n--------------拷贝完成_____\n\n\n\n\n\n\n\n\n\n");
        C_LogTxtSave.S_ClearAll();
    }

}
